#include <built_in.h>
#include "chars.h"
#include "../shared/char_addrs.h"
#include "../controller/GLCD/GLCD.h"

void SETUP_CHAR_init(){
  I2C_Init(100000);
}

//
// Write a single display character to the external storage
//
void SETUP_CHAR_writeDisplayCharacter(char keycode, char* data){

  char i;
  int address = MEMMAP_POS_CHAR + (keycode - CHAR_FIRST_ADDRESS) * 8;
  
  I2C_Start();
  I2C_Wr(MEMMAP_ADDR_CHARACTER_CHIP); // Device address + Write
  I2C_Wr(Hi(address));   // High order byte of address
  I2C_Wr(Lo(address));   // Low order byte of address
  for( i=0; i<8; i++){
    I2C_Wr(data[i]);     // Write data
  }
  I2C_Stop();
  
  delay_ms(100); // to prevent overloading the memory circuit.
}

void SETUP_CHAR_loadGridChars(){

  char cGridHead[]    = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F};
  char cGridHeadC[]   = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
  char cGridHeadL[]   = {0x00, 0x00, 0x00, 0x0F, 0x08, 0x08, 0x00, 0x3F};
  char cGridHeadM[]   = {0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x3F};
  char cGridHeadR[]   = {0x00, 0x00, 0x00, 0x3C, 0x04, 0x04, 0x00, 0x3F};

  char cGridHead0[]   = {0x00, 0x0C, 0x12, 0x12, 0x12, 0x0C, 0x00, 0x3F};
  char cGridHead1[]   = {0x00, 0x04, 0x0C, 0x04, 0x04, 0x04, 0x00, 0x3F};
  char cGridHead2[]   = {0x00, 0x1C, 0x02, 0x0C, 0x10, 0x1E, 0x00, 0x3F};
  char cGridHead3[]   = {0x00, 0x1C, 0x02, 0x04, 0x02, 0x1C, 0x00, 0x3F};
  char cGridHead4[]   = {0x00, 0x10, 0x12, 0x1E, 0x02, 0x02, 0x00, 0x3F};
  char cGridHead5[]   = {0x00, 0x1E, 0x10, 0x1C, 0x02, 0x1C, 0x00, 0x3F};
  char cGridHead6[]   = {0x00, 0x0C, 0x10, 0x1C, 0x12, 0x0C, 0x00, 0x3F};
  char cGridHead7[]   = {0x00, 0x1E, 0x02, 0x04, 0x08, 0x08, 0x00, 0x3F};
  char cGridHead8[]   = {0x00, 0x0C, 0x12, 0x0C, 0x12, 0x0C, 0x00, 0x3F};
  char cGridHead9[]   = {0x00, 0x0C, 0x12, 0x0E, 0x02, 0x0C, 0x00, 0x3F};
  char cGridHead10[]   = {0x00, 0x12, 0x35, 0x15, 0x15, 0x12, 0x00, 0x3F};

  char cGridL[]       = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
  char cGridMOn[]     = {0x00, 0x09, 0x1C, 0x1D, 0x1C, 0x09, 0x00, 0x15};
  char cGridMOff[]    = {0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x15};
  char cGridROn[]     = {0x01, 0x09, 0x1D, 0x1D, 0x1D, 0x09, 0x01, 0x15};
  char cGridROff[]    = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x15};

  char cGridROnInv[]  = {0x3E, 0x36, 0x22, 0x22, 0x22, 0x36, 0x3E, 0x15};
  char cGridROffInv[] = {0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x15};
  char cGridMOnInv[]  = {0x3E, 0x37, 0x22, 0x23, 0x22, 0x37, 0x3E, 0x15};
  char cGridMOffInv[] = {0x3E, 0x3F, 0x3E, 0x3F, 0x3E, 0x3F, 0x3E, 0x15};
  
  char cGridBar1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE};
  char cGridBar2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE};
  char cGridBar3[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFE};
  char cGridBar4[] = {0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFE, 0xFE};
  char cGridBar5[] = {0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE};
  char cGridBar6[] = {0x00, 0x00, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE};
  char cGridBar7[] = {0x00, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE};
  char cGridBar8[] = {0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE};

  SETUP_CHAR_writeDisplayCharacter(gridHead,  cGridHead);
  SETUP_CHAR_writeDisplayCharacter(gridHeadC, cGridHeadC);

  SETUP_CHAR_writeDisplayCharacter(gridHead0, cGridHead0);
  SETUP_CHAR_writeDisplayCharacter(gridHead1, cGridHead1);
  SETUP_CHAR_writeDisplayCharacter(gridHead2, cGridHead2);
  SETUP_CHAR_writeDisplayCharacter(gridHead3, cGridHead3);
  SETUP_CHAR_writeDisplayCharacter(gridHead4, cGridHead4);
  SETUP_CHAR_writeDisplayCharacter(gridHead5, cGridHead5);
  SETUP_CHAR_writeDisplayCharacter(gridHead6, cGridHead6);
  SETUP_CHAR_writeDisplayCharacter(gridHead7, cGridHead7);
  SETUP_CHAR_writeDisplayCharacter(gridHead8, cGridHead8);
  SETUP_CHAR_writeDisplayCharacter(gridHead9, cGridHead9);
  SETUP_CHAR_writeDisplayCharacter(gridHead10, cGridHead10);

  SETUP_CHAR_writeDisplayCharacter(gridL,    cGridL);
  SETUP_CHAR_writeDisplayCharacter(gridMOn,  cGridMOn);
  SETUP_CHAR_writeDisplayCharacter(gridMOff, cGridMOff);
  SETUP_CHAR_writeDisplayCharacter(gridROn,  cGridROn);
  SETUP_CHAR_writeDisplayCharacter(gridROff, cGridROff);

  SETUP_CHAR_writeDisplayCharacter(gridMOnInv,  cGridMOnInv);
  SETUP_CHAR_writeDisplayCharacter(gridMOffInv, cGridMOffInv);
  SETUP_CHAR_writeDisplayCharacter(gridROnInv,  cGridROnInv);
  SETUP_CHAR_writeDisplayCharacter(gridROffInv, cGridROffInv);

  SETUP_CHAR_writeDisplayCharacter(gridBar1, cGridBar1);
  SETUP_CHAR_writeDisplayCharacter(gridBar2, cGridBar2);
  SETUP_CHAR_writeDisplayCharacter(gridBar3, cGridBar3);
  SETUP_CHAR_writeDisplayCharacter(gridBar4, cGridBar4);
  SETUP_CHAR_writeDisplayCharacter(gridBar5, cGridBar5);
  SETUP_CHAR_writeDisplayCharacter(gridBar6, cGridBar6);
  SETUP_CHAR_writeDisplayCharacter(gridBar7, cGridBar7);
  SETUP_CHAR_writeDisplayCharacter(gridBar8, cGridBar8);
} //loadGridChars


void SETUP_CHAR_loadInstrumentChars(){

  //Instruments/track names
  char cInsSnare1[] = {0x00, 0x18, 0x23, 0x12, 0x0A, 0x32, 0x00, 0x00 };
  char cInsSnare2[] = {0x00, 0x00, 0x04, 0x2A, 0x2E, 0x2A, 0x00, 0x00 };
  char cInsSnare3[] = {0x00, 0x00, 0x29, 0x32, 0x23, 0x21, 0x00, 0x00 };
  char cInsSnare4[] = {0x00, 0x00, 0x00, 0x20, 0x00, 0x20, 0x00, 0x00 };

  char cInsBass1[] = {0x00, 0x30, 0x29, 0x32, 0x2B, 0x32, 0x00, 0x00};
  char cInsBass2[] = {0x00, 0x00, 0x04, 0x29, 0x24, 0x29, 0x00, 0x00};
  char cInsBass3[] = {0x00, 0x00, 0x20, 0x00, 0x20, 0x00, 0x00, 0x00};

  char cInsHigh1[] = {0x00, 0x2A, 0x28, 0x3A, 0x2A, 0x2A, 0x00, 0x00};
  char cInsHigh2[] = {0x00, 0x02, 0x12, 0x2B, 0x1A, 0x0A, 0x30, 0x00};
  char cInsHigh3[] = {0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x00, 0x00};

  char cInsMid1[] = {0x00, 0x22, 0x36, 0x2A, 0x22, 0x22, 0x00, 0x00};
  char cInsMid2[] = {0x00, 0x22, 0x02, 0x26, 0x2A, 0x26, 0x00, 0x00};

  char cInsLow1[] = {0x00, 0x20, 0x21, 0x22, 0x22, 0x39, 0x00, 0x00};
  char cInsLow2[] = {0x00, 0x00, 0x08, 0x2A, 0x2A, 0x05, 0x00, 0x00};
  char cInsLow3[] = {0x00, 0x00, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00};

  char cInsCrash1[] = {0x00, 0x18, 0x22, 0x23, 0x22, 0x1A, 0x00, 0x00};
  char cInsCrash2[] = {0x00, 0x00, 0x24, 0x0A, 0x0E, 0x0A, 0x00, 0x00};
  char cInsCrash3[] = {0x00, 0x04, 0x14, 0x26, 0x15, 0x25, 0x00, 0x00};

  char cInsRide1[] = {0x00, 0x32, 0x28, 0x32, 0x2A, 0x2A, 0x00, 0x00};
  char cInsRide2[] = {0x00, 0x08, 0x09, 0x1A, 0x2B, 0x19, 0x00, 0x00};
  char cInsRide3[] = {0x00, 0x00, 0x00, 0x20, 0x00, 0x20, 0x00, 0x00};

  char cInsHH1[] = {0x00, 0x2A, 0x2A, 0x3B, 0x2A, 0x2A, 0x00, 0x00};
  char cInsHH2[] = {0x00, 0x20, 0x22, 0x25, 0x25, 0x22, 0x00, 0x00}; //open
  char cInsHH3[] = {0x00, 0x00, 0x18, 0x15, 0x15, 0x18, 0x10, 0x00}; //open
  char cInsHH4[] = {0x00, 0x00, 0x26, 0x15, 0x25, 0x35, 0x00, 0x00}; //open

  char cInsHH5[] = {0x00, 0x20, 0x22, 0x24, 0x24, 0x22, 0x00, 0x00}; //closed
  char cInsHH6[] = {0x00, 0x20, 0x24, 0x2A, 0x2A, 0x24, 0x00, 0x00}; //closed
  char cInsHH7[] = {0x00, 0x00, 0x12, 0x25, 0x16, 0x23, 0x00, 0x00}; //closed
  char cInsHH8[] = {0x00, 0x04, 0x04, 0x0C, 0x14, 0x0C, 0x00, 0x00}; //closed

  char cInsRim1[] = {0x00, 0x32, 0x28, 0x32, 0x2A, 0x2A, 0x00, 0x00};
  char cInsRim2[] = {0x00, 0x00, 0x34, 0x2A, 0x2A, 0x2A, 0x00, 0x00};
  char cInsRim3[] = {0x00, 0x04, 0x14, 0x26, 0x15, 0x25, 0x00, 0x00};
  char cInsRim4[] = {0x00, 0x01, 0x09, 0x15, 0x15, 0x08, 0x00, 0x00};
  char cInsRim5[] = {0x00, 0x00, 0x00, 0x20, 0x00, 0x20, 0x00, 0x00};

  char cInsHand1[] = {0x00, 0x28, 0x29, 0x3A, 0x2B, 0x2A, 0x00, 0x00};
  char cInsHand2[] = {0x00, 0x00, 0x0c, 0x2A, 0x2A, 0x2A, 0x00, 0x00};
  char cInsHand3[] = {0x00, 0x08, 0x19, 0x2A, 0x2A, 0x19, 0x00, 0x00};
  char cInsHand4[] = {0x00, 0x10, 0x12, 0x15, 0x17, 0x15, 0x00, 0x00};
  char cInsHand5[] = {0x00, 0x00, 0x18, 0x14, 0x14, 0x18, 0x10, 0x00};

  char cInsPatt1[] = {0x00, 0x30, 0x29, 0x32, 0x23, 0x22, 0x00, 0x00};
  char cInsPatt2[] = {0x00, 0x09, 0x09, 0x2D, 0x29, 0x24, 0x00, 0x00};
  char cInsPatt3[] = {0x00, 0x00, 0x04, 0x2A, 0x0C, 0x26, 0x00, 0x00};
  char cInsPatt4[] = {0x00, 0x00, 0x2B, 0x32, 0x22, 0x22, 0x00, 0x00};
  char cInsPatt5[] = {0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x00, 0x00};

  char cInsGen1[] = {0x00, 0x18, 0x21, 0x22, 0x2B, 0x19, 0x00, 0x00};
  char cInsGen2[] = {0x00, 0x00, 0x0C, 0x2A, 0x0A, 0x2A, 0x00, 0x00};
  char cInsGen3[] = {0x00, 0x00, 0x12, 0x2B, 0x32, 0x1A, 0x00, 0x00};
  char cInsGen4[] = {0x00, 0x00, 0x24, 0x0A, 0x0E, 0x0A, 0x00, 0x00};
  char cInsGen5[] = {0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00};

  char cInsTrack1[] = {0x00, 0x38, 0x12, 0x13, 0x12, 0x12, 0x00, 0x00};
  char cInsTrack2[] = {0x00, 0x00, 0x24, 0x0A, 0x0E, 0x0A, 0x00, 0x00};
  char cInsTrack3[] = {0x00, 0x04, 0x15, 0x26, 0x25, 0x15, 0x00, 0x00};

  char cIns0[] = {0x00, 0x02, 0x05, 0x05, 0x05, 0x02, 0x00, 0x00};
  char cIns1[] = {0x00, 0x02, 0x06, 0x02, 0x02, 0x07, 0x00, 0x00};
  char cIns2[] = {0x00, 0x06, 0x01, 0x02, 0x04, 0x07, 0x00, 0x00};
  char cIns3[] = {0x00, 0x06, 0x01, 0x02, 0x01, 0x06, 0x00, 0x00};
  char cIns4[] = {0x00, 0x04, 0x05, 0x07, 0x01, 0x01, 0x00, 0x00};
  char cIns5[] = {0x00, 0x07, 0x04, 0x06, 0x01, 0x06, 0x00, 0x00};
  char cIns6[] = {0x00, 0x02, 0x04, 0x06, 0x05, 0x02, 0x00, 0x00};
  char cIns7[] = {0x00, 0x07, 0x01, 0x02, 0x02, 0x02, 0x00, 0x00};
  char cIns8[] = {0x00, 0x02, 0x05, 0x02, 0x05, 0x02, 0x00, 0x00};
  char cIns9[] = {0x00, 0x02, 0x05, 0x03, 0x01, 0x02, 0x00, 0x00};

  SETUP_CHAR_writeDisplayCharacter(insSnare1, cInsSnare1);
  SETUP_CHAR_writeDisplayCharacter(insSnare2, cInsSnare2);
  SETUP_CHAR_writeDisplayCharacter(insSnare3, cInsSnare3);
  SETUP_CHAR_writeDisplayCharacter(insSnare4, cInsSnare4);

  SETUP_CHAR_writeDisplayCharacter(insBass1, cInsBass1);
  SETUP_CHAR_writeDisplayCharacter(insBass2, cInsBass2);
  SETUP_CHAR_writeDisplayCharacter(insBass3, cInsBass3);

  SETUP_CHAR_writeDisplayCharacter(insHigh1, cInsHigh1);
  SETUP_CHAR_writeDisplayCharacter(insHigh2, cInsHigh2);
  SETUP_CHAR_writeDisplayCharacter(insHigh3, cInsHigh3);

  SETUP_CHAR_writeDisplayCharacter(insMid1, cInsMid1);
  SETUP_CHAR_writeDisplayCharacter(insMid2, cInsMid2);

  SETUP_CHAR_writeDisplayCharacter(insLow1, cInsLow1);
  SETUP_CHAR_writeDisplayCharacter(insLow2, cInsLow2);
  SETUP_CHAR_writeDisplayCharacter(insLow3, cInsLow3);

  SETUP_CHAR_writeDisplayCharacter(insCrash1, cInsCrash1);
  SETUP_CHAR_writeDisplayCharacter(insCrash2, cInsCrash2);
  SETUP_CHAR_writeDisplayCharacter(insCrash3, cInsCrash3);

  SETUP_CHAR_writeDisplayCharacter(insRide1, cInsRide1);
  SETUP_CHAR_writeDisplayCharacter(insRide2, cInsRide2);
  SETUP_CHAR_writeDisplayCharacter(insRide3, cInsRide3);

  SETUP_CHAR_writeDisplayCharacter(insHH1, cInsHH1);
  SETUP_CHAR_writeDisplayCharacter(insHH2, cInsHH2);
  SETUP_CHAR_writeDisplayCharacter(insHH3, cInsHH3);
  SETUP_CHAR_writeDisplayCharacter(insHH4, cInsHH4);
  SETUP_CHAR_writeDisplayCharacter(insHH5, cInsHH5);
  SETUP_CHAR_writeDisplayCharacter(insHH6, cInsHH6);
  SETUP_CHAR_writeDisplayCharacter(insHH7, cInsHH7);
  SETUP_CHAR_writeDisplayCharacter(insHH8, cInsHH8);

  SETUP_CHAR_writeDisplayCharacter(insRim1, cInsRim1);
  SETUP_CHAR_writeDisplayCharacter(insRim2, cInsRim2);
  SETUP_CHAR_writeDisplayCharacter(insRim3, cInsRim3);
  SETUP_CHAR_writeDisplayCharacter(insRim4, cInsRim4);
  SETUP_CHAR_writeDisplayCharacter(insRim5, cInsRim5);

  SETUP_CHAR_writeDisplayCharacter(insHand1, cInsHand1);
  SETUP_CHAR_writeDisplayCharacter(insHand2, cInsHand2);
  SETUP_CHAR_writeDisplayCharacter(insHand3, cInsHand3);
  SETUP_CHAR_writeDisplayCharacter(insHand4, cInsHand4);
  SETUP_CHAR_writeDisplayCharacter(insHand5, cInsHand5);

  SETUP_CHAR_writeDisplayCharacter(insPatt1, cInsPatt1);
  SETUP_CHAR_writeDisplayCharacter(insPatt2, cInsPatt2);
  SETUP_CHAR_writeDisplayCharacter(insPatt3, cInsPatt3);
  SETUP_CHAR_writeDisplayCharacter(insPatt4, cInsPatt4);
  SETUP_CHAR_writeDisplayCharacter(insPatt5, cInsPatt5);

  SETUP_CHAR_writeDisplayCharacter(insGen1, cInsGen1);
  SETUP_CHAR_writeDisplayCharacter(insGen2, cInsGen2);
  SETUP_CHAR_writeDisplayCharacter(insGen3, cInsGen3);
  SETUP_CHAR_writeDisplayCharacter(insGen4, cInsGen4);
  SETUP_CHAR_writeDisplayCharacter(insGen5, cInsGen5);
  
  SETUP_CHAR_writeDisplayCharacter(insTrack1, cInsTrack1);
  SETUP_CHAR_writeDisplayCharacter(insTrack2, cInsTrack2);
  SETUP_CHAR_writeDisplayCharacter(insTrack3, cInsTrack3);
  
  SETUP_CHAR_writeDisplayCharacter(ins0, cIns0);
  SETUP_CHAR_writeDisplayCharacter(ins1, cIns1);
  SETUP_CHAR_writeDisplayCharacter(ins2, cIns2);
  SETUP_CHAR_writeDisplayCharacter(ins3, cIns3);
  SETUP_CHAR_writeDisplayCharacter(ins4, cIns4);
  SETUP_CHAR_writeDisplayCharacter(ins5, cIns5);
  SETUP_CHAR_writeDisplayCharacter(ins6, cIns6);
  SETUP_CHAR_writeDisplayCharacter(ins7, cIns7);
  SETUP_CHAR_writeDisplayCharacter(ins8, cIns8);
  SETUP_CHAR_writeDisplayCharacter(ins9, cIns9);
} //loadInstrumentChars

/**
 * misc chars
 **/
void SETUP_CHAR_loadSpecialChars(){

  char cRightArrow[] = {0x00, 0x10, 0x18, 0x1C, 0x18, 0x10, 0x00, 0x00};
  char cLeftArrow[]  = {0x00, 0x02, 0x06, 0x0E, 0x06, 0x02, 0x00, 0x00};
  char cDivider[]    = {0x00 ,0x08, 0x00, 0x00, 0x08, 0x00, 0x00,0x08};

  char cSolo1[]    = {0x00, 0x03, 0x04, 0x03, 0x00, 0x07, 0x00, 0x00};
  char cSolo2[]    = {0x01, 0x31, 0x01, 0x21, 0x11, 0x21, 0x01, 0x01};
  char cMute1[]    = {0x00, 0x04, 0x06, 0x05, 0x04, 0x04, 0x00, 0x00};
  char cMute2[]    = {0x01, 0x11, 0x31, 0x11, 0x11, 0x11, 0x01, 0x01};

  char cSetupLineTop[]  = {0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00};
  char cSetupLineTopDotted[]  = {0x00, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00};
  char cSetupLineTopTDotted[]  = {0x00, 0x00, 0x2A, 0x00, 0x20, 0x00, 0x20, 0x00};
  
//  char cSetupLine[]  = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20};
  char cSetupLine[]  = {0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00};

  char cPlay[]  = {0x00, 0x20, 0x38, 0x3E, 0x38, 0x20, 0x00, 0x00};
  char cStop[]  = {0x00, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x00, 0x00};
  char cPause[] = {0x00, 0x36, 0x36, 0x36, 0x36, 0x36, 0x00, 0x00};
  char cRec[]   = {0x00, 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, 0x00};
  char cExtSynch[] = {0x00, 0x3E, 0x20, 0x3C, 0x20, 0x3E, 0x00, 0x00};
  char cLoop[]  = {0x00, 0x20, 0x20, 0x20, 0x20, 0x3E, 0x00, 0x00};
  char cNotok[]  = {0x00, 0x22, 0x14, 0x08, 0x14, 0x22, 0x00, 0x00};
  
  SETUP_CHAR_writeDisplayCharacter(rightArrow, cRightArrow);
  SETUP_CHAR_writeDisplayCharacter(leftArrow, cLeftArrow);
  SETUP_CHAR_writeDisplayCharacter(divider, cDivider);

  SETUP_CHAR_writeDisplayCharacter(solo1, cSolo1);
  SETUP_CHAR_writeDisplayCharacter(solo2, cSolo2);
  SETUP_CHAR_writeDisplayCharacter(mute1, cMute1);
  SETUP_CHAR_writeDisplayCharacter(mute2, cMute2);

  SETUP_CHAR_writeDisplayCharacter(setupLine, cSetupLine);
  SETUP_CHAR_writeDisplayCharacter(setupLineTop, cSetupLineTop);
  SETUP_CHAR_writeDisplayCharacter(setupLineTopDotted, cSetupLineTopDotted);
  SETUP_CHAR_writeDisplayCharacter(setupLineTopTDotted, cSetupLineTopTDotted);

  SETUP_CHAR_writeDisplayCharacter(play, cPlay);
  SETUP_CHAR_writeDisplayCharacter(stop, cStop);
  SETUP_CHAR_writeDisplayCharacter(pause, cPause);
  SETUP_CHAR_writeDisplayCharacter(rec, cRec);
  SETUP_CHAR_writeDisplayCharacter(extSynch, cExtSynch);
  SETUP_CHAR_writeDisplayCharacter(loop, cLoop);
  SETUP_CHAR_writeDisplayCharacter(notok, cNotok);
}

//
// Flam illustration characters
//
void SETUP_CHAR_loadFlamChars(){

  char cFlam0[] = {0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x00, 0x2A}; // 16 / 8 tri 1
  char cFlam1[] = {0x00, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x2A}; // 8 tri 2
  char cFlam2[] = {0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x2A}; // 8 tri 3
  char cFlam3[] = {0x00, 0x00, 0x00, 0x22, 0x22, 0x22, 0x00, 0x2A}; // 16 tri 1
  char cFlam4[] = {0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x00, 0x2A}; // 16 tri 2
  char cFlam5[] = {0x00, 0x00, 0x00, 0x24, 0x24, 0x24, 0x00, 0x2A}; // 32
  char cFlam6[] = {0x00, 0x00, 0x00, 0x2A, 0x2A, 0x2A, 0x00, 0x2A}; // 32 tri
  
  SETUP_CHAR_writeDisplayCharacter(flam0, cFlam0);
  SETUP_CHAR_writeDisplayCharacter(flam1, cFlam1);
  SETUP_CHAR_writeDisplayCharacter(flam2, cFlam2);
  SETUP_CHAR_writeDisplayCharacter(flam3, cFlam3);
  SETUP_CHAR_writeDisplayCharacter(flam4, cFlam4);
  SETUP_CHAR_writeDisplayCharacter(flam5, cFlam5);
  SETUP_CHAR_writeDisplayCharacter(flam6, cFlam6);
}

//
// Border characters for dialog boxes
//
void SETUP_CHAR_loadBorderChars(){
  /*
  char cBorderUL[] = {0x3F, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20};
  char cBorderUM[] = {0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  char cBorderUR[] = {0x3F, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
  */
  
  char cBorderUL[] = {0x3F, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20};
  char cBorderUM[] = {0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  char cBorderUR[] = {0x3F, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};

  char cBorderL[]  = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20};
  char cBorderR[]  = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
  char cBorderLL[] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3F};
  char cBorderLM[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F};
  char cBorderLR[] = {0x01, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00};

  SETUP_CHAR_writeDisplayCharacter(borderUL, cBorderUL);
  SETUP_CHAR_writeDisplayCharacter(borderUM, cBorderUM);
  SETUP_CHAR_writeDisplayCharacter(borderUR, cBorderUR);
  SETUP_CHAR_writeDisplayCharacter(borderL,  cBorderL);
  SETUP_CHAR_writeDisplayCharacter(borderR,  cBorderR);
  SETUP_CHAR_writeDisplayCharacter(borderLL, cBorderLL);
  SETUP_CHAR_writeDisplayCharacter(borderLM, cBorderLM);
  SETUP_CHAR_writeDisplayCharacter(borderLR, cBorderLR);
}

//
// read a single display character from the external storage
//
void SETUP_CHAR_readDisplayCharacter(char keycode, char* data){

  char i;
  int address = MEMMAP_POS_CHAR + (keycode - CHAR_FIRST_ADDRESS) * 8;

  I2C_Start();
  I2C_Wr(MEMMAP_ADDR_CHARACTER_CHIP);          // Device address + Write
  I2C_Wr(Hi(address));   // High order byte of address
  I2C_Wr(Lo(address));   // Low order byte of address
  I2C_Repeated_Start();
  I2C_Wr(MEMMAP_ADDR_CHARACTER_CHIP+1);          // Device address + Read
  for( i=0; i<8; i++){
    if(i!=7){
      data[i] = I2C_Rd(1u); // Read and send ack
    } else {
      data[i] = I2C_Rd(0u); // Read but don't send ack
    }
  }

  I2C_Stop();
}

void SETUP_CHAR_test(){
  char data[8];
  SETUP_CHAR_readDisplayCharacter(gridHead, data);

  if(data[7] == 0x3F){
    PORTE.F1 = 1;
  }
}